<?php
declare (strict_types = 1);

namespace app\admin\service;
use app\admin\model\UserModel;
use thans\jwt\facade\JWTAuth;


class UserService extends \think\Service
{
    /**
     * 注册服务
     *
     * @return mixed
     */
    public function register()
    {
    	//
    }

    /**
     * 执行服务
     *
     * @return mixed
     */
    public function boot()
    {
        //
    }

    /**
     * Method index
     * Explain:index
     * Params:
     * @param $params
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * user:lijing
     * time:2024/2/26 23:54
     */
    public function index($params): \think\response\Json
    {
        $query = UserModel::query()->when($params['search_text'] , function($query , $value){
            $query->where('user_name' , 'like',sprintf('%%%s%%', $value));
        });

        $total = $query->count();
        $list = $query->page($params['pageNum'] , $params['pageSize'])->select()->toArray();

        return successMsg(data:makePaginate($list , $total ,$params));
    }

    /**
     * Method getUser
     * Explain:getUser
     * Params:
     * @param string $account
     * @param string $pass
     * user:lijing
     * time:2024/2/21 23:45
     */
    public function login(string $account , string $password)
    {

        $user = UserModel::where('account' , $account)->where('password' , md5($password))->find();

        if($user){
            $token = JWTAuth::builder(
                [
                    'user_id' => $user->id,
                    'user_role' => $user->role
                ]
            );//参数为用户认证的信息，请自行添加
            return successMsg(data: ['access_token'=>$token]);
        }
        return errMsg(msg: '账号或密码错误');
    }

    /**
     * @param $params
     * @return \think\response\Json
     */
    public function createUser($params): \think\response\Json
    {
        if(isset($params['password'])) $params['password'] = md5($params['password']);
        $res = UserModel::create($params);
        return successMsg(data: $res);
    }

    /**
     * @param $id
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getUser($id): mixed
    {
        return UserModel::find($id);
    }

    /**
     * @param $params
     * @return UserModel
     */
    public function updateUser($params): UserModel
    {
        $id = $params['id'];
        if(isset($params['password'])){
            $params['password'] = md5($params['password']);
        }
        unset($params['id']);
        return UserModel::update($params, ['id' => $id]);
    }

    /**
     * @return \think\response\Json
     * 退出登录
     */
    public function logout(): \think\response\Json
    {
        $token = JWTAuth::token()->get();
        JWTAuth::invalidate($token);
        return successMsg();
    }

    /**
     * Method deleteUser
     * Explain:deleteUser
     * Params:
     * @param $id
     * @return \think\response\Json
     * user:lijing
     * time:2024/2/22 22:56
     */
    public function deleteUser($id): \think\response\Json
    {
        UserModel::destroy($id);
        return successMsg();
    }
}
